package com.cos.hospital.ui.login;

import android.app.Activity;
import android.content.Intent;

import com.cos.hospital.code.Code;
import com.cos.hospital.dao.HelperInfoDb;
import com.cos.hospital.dao.UCheckDB;
import com.cos.hospital.dao.beandao.DbInfo;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.bean.user.SessionBean;
import com.cos.hospital.model.bean.user.TokenBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.model.retrofit.RequestServes;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UDialog;
import com.cos.hospital.tool.USP;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.main.MainActivity;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @Description: <p>
 * @Author: Kosmos
 * @Date: 2018年12月23日 00:08
 * @Email: KosmoSakura@foxmail.com
 */
class ULogin {
    private RequestServes serves;
    private String phone;
    private String password;
    private Activity act;
    private CompositeDisposable compositeDisposable;

    ULogin(Activity act, RequestServes serves, String phone,
           String password) {
        this.serves = serves;
        this.phone = phone;
        this.password = password;
        this.act = act;
    }

    void onDestroy() {
        if (compositeDisposable != null) {
            compositeDisposable.clear();
        }
    }

    void runLogin() {
        ProgressDialog.getInstance().startProgressDialog(act, "登录中...");
        UHttp.start(serves.getToken("password", "app", "Doctor",
            "201810", phone, password), "获取token", new HttpMsgListener<TokenBean>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                ProgressDialog.getInstance().stopProgressDialog();
                showNotice(describe);
            }

            @Override
            public void onSuccess(TokenBean dto) {
                if (UTxt.isEmpty(dto.getError())) {
                    SPUtil.saveToken(dto);
                    USP.instance().putString(Code.SP.USER_NAME, phone);
//                    USP.instance().putString(Code.SP.USER_PWD, password);
                    getUserSession();
                } else {
                    ProgressDialog.getInstance().stopProgressDialog();
                    showNotice(UTxt.isNull(dto.getError_description(), Code.DefaultPrompt));
                }
            }
        });
    }

    private void getUserSession() {
        UHttp.start(serves.getUserSession(new JsonObject()), "获取Session",
            new HttpMsgListener<ResultBean<SessionBean>>() {
                @Override
                public void onSubscribe(Disposable disposable) {
                    addEvent(disposable);
                }

                @Override
                public void onError(String describe) {
                    showNotice(describe);
                }

                @Override
                public void onSuccess(ResultBean<SessionBean> dto) {
                    if (dto.isSuccess()) {
                        SessionBean resultBean = dto.getResult();
                        if (resultBean != null) {
                            int userId = resultBean.getUserId();
                            SPUtil.saveId(userId);
                            DbInfo info = new DbInfo((long) userId);
                            info.setTelephone(resultBean.getUserName());
                            HelperInfoDb.insert(act, info);
                            checkDB();
                        }
                    } else {
                        ProgressDialog.getInstance().stopProgressDialog();
                        ErrorBean errorBean = dto.getError();
                        if (errorBean != null) {
                            showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                        } else {
                            showNotice(Code.DefaultPrompt);
                        }
                    }
                }
            });
    }

    private void checkDB() {
        UCheckDB.check(act, serves, new UCheckDB.CheckListener() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onEnd() {
                ProgressDialog.getInstance().stopProgressDialog();
                act.startActivity(new Intent(act, MainActivity.class));
                act.finish();
            }
        }).checkVersion("登录中...");
    }

    private void addEvent(Disposable disposable) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }


    private void showNotice(String notice) {
        UDialog.getInstance(act, false, false)
            .showNoticeWithOnebtn(notice);
    }
}
